// pages/near/near.js
const utils = require('../../utils/util.js')
const getLocation = utils.wxPromisify(wx.getLocation)
const getSystemInfo = utils.wxPromisify(wx.getSystemInfo)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 'nearby',
    longitude:'',
    latitude:'',
    height:'',
    markers:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getLocation({
      type:'wgs84'
    }).then(res => {
      const { longitude, latitude} = res
      let markers = []
      for (let i = 0;i < 10; i++){
        const marker = {
          id:i,
          latitude:latitude + this.randomRange(-0.05,0.05),
          longitude: longitude + this.randomRange(-0.05,0.05),
          iconPath:'/images/umbrella.png',
        }
        markers.push(marker)
      }

      this.setData({
        longitude,
        latitude,
        markers
      })
    })

    getSystemInfo().then(res => {
      this.setData({
        height: res.windowHeight - 50
      })
    })

  },
  daohang(event){
    const marker = this.data.markers[event.markerId]
    wx.openLocation({
      ...marker
    })
  },
  saoInfo(){

    wx.getUserInfo({
      success: function(res) {
        console.log(res)
        wx.scanCode({
          scanType: [],
          success: function (res) {
            console.log(res)
          },
          fail: function (res) { },
          complete: function (res) { },
        })
      }
    })

  },
  randomRange(myMin, myMax) {
    return Math.random() * (myMax - myMin) + myMin;
  },
  handleChange({ detail }) {
    if (detail.key == 'index') {
      wx.redirectTo({
        url: '/pages/index/index',
      })
    }
    this.setData({
      current: detail.key
    })

  },
  toIndex(){
    wx.redirectTo({
      url: '/pages/index/index',
    })
  }
})